<?php

namespace App\Http\Controllers\Estate;

use App\Http\Controllers\Controller;
use App\Models\ElectricAlarmRecord;
use App\Models\ElectricPlatformOwner;
use Illuminate\Http\Request;

class AlarmRecordController extends Controller
{
    public function index(Request $request)
    {
        $where = [];
        $where[] = ['user_id', '<>', 0];

        $mobile = $request->get('mobile');
        if ($mobile) {
            //查询用户
            $id = ElectricPlatformOwner::query()->where('mobile', $mobile)->value('id');
            $where[] = ['user_id', '=', $id];
        }

        $limit = $request->get('limit', 20);

        $data = ElectricAlarmRecord::query()->with('user:id,room_number,username,mobile')->whereNotNull(['user_id', 'code'])->where($where)->orderByDesc('id')->paginate($limit);
        return $this->success($data);
    }
}
